package hunglv.android.rss;

import java.util.ArrayList;
import java.util.List;

import hunglv.android.rss.entities.RSSFeed;
import hunglv.android.rss.entities.RSSItem;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemClickListener{
	private ListView feed_items_lisview;
	private ProgressDialog mProgressDialog;

	private ArrayAdapter<RSSItem> feed_items_adapter;
	private List<RSSItem> feed_items_list;

	private FeedRSSAsyncTask mFeedRSSAsyncTask;

	public static Handler mHandler;

	public static final int NOTIFY_FINISH = 1;
	public static final String FEED_URL = "http://rss.cnn.com/rss/edition_europe.rss";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//get view
		feed_items_lisview = (ListView) findViewById(R.id.feed_items_listview);
		feed_items_lisview.setOnItemClickListener(this);

		//initialize
		feed_items_list = new ArrayList<RSSItem>();

		//binding listview data to feed_items_list
		feed_items_adapter = new ArrayAdapter<RSSItem>(this,
				android.R.layout.simple_list_item_1, feed_items_list);
		feed_items_lisview.setAdapter(feed_items_adapter);

		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.show();

		// retrieve rss data
		mFeedRSSAsyncTask = new FeedRSSAsyncTask();
		mFeedRSSAsyncTask.execute(FEED_URL);
		
		//handle message
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				int what = msg.what;
				if (what == NOTIFY_FINISH) {
					mProgressDialog.hide();
					RSSFeed mRSSFeed = mFeedRSSAsyncTask.getFeed();
					
					if(mRSSFeed == null){ // no network connection
						
					}else if(mRSSFeed.getAllItems().size() == 0){ // can't retrieve rss data
						
					}
					else if(mRSSFeed != null && mRSSFeed.getAllItems().size() > 0){
						feed_items_list.clear();
						for (RSSItem item : mRSSFeed.getAllItems()) {
							feed_items_list.add(item);
						}
						feed_items_adapter.notifyDataSetChanged();
					}
				}
			}
		};
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
			Log.v("RSS", "position: "+pos);
			RSSItem item = feed_items_list.get(pos);
			
			Bundle mBundle = new Bundle();
			mBundle.putString("title", item.getTitle());
			mBundle.putString("description", item.getDescription());
			mBundle.putString("link", item.getLink());
			mBundle.putString("pubdate", item.getPubDate());
			
			Intent intent = new Intent(MainActivity.this, DescriptionActivity.class);
			intent.putExtra("hunglv.android.rss.rss_item", mBundle);
			
			startActivity(intent);
	}
}
